/**
 * 
 */
package pacMan;

import java.awt.Graphics2D;

/**
 * 
 * Class: SuperpositionedStationaryObject
 * This is an object that can be referenced in multiple discrete locations in the
 * ObjectLocationTracker, which allows for us to assign it to multiple locations on
 * the game board and draw the same image in all locations with a single call. Useful
 * for cases where we need to draw lots of identical images without having to create
 * an object to track the location for each one. Each image will be drawn at identical
 * offsets within the gameboard 'tile'.
 *
 * @author Tomas Rivera
 * @version $Id$
 */
public abstract class SuperpositionedStationaryObject extends ConsumableObject {
	
	//////////////////////////////////////////////////////////
	// Constructor methods
	//////////////////////////////////////////////////////////
	
	/**
	 * A superpositioned object is one that exists in multiple discrete locations,
	 * as opposed to one that spans multiple locations (such as a Wall).
	 * 
	 * @param w
	 * @param pValue
	 * @param objectLocationTracker
	 */
	public SuperpositionedStationaryObject(WindowParameters w, int pValue,  ObjectLocationTracker objectLocationTracker) {
		super(null, w, pValue);
		this.objectLocationTracker = objectLocationTracker;
		generatedCount = visibleCount = 0;
	}
	
	//////////////////////////////////////////////////////////
	// Abstract methods
	//////////////////////////////////////////////////////////
	
	/**
	 * Draws the superpositioned object at all positions where it is currently placed on the game board.
	 * 
	 * @param g2d
	 * @param w
	 * @param xGrid
	 * @param yGrid
	 */
	public abstract void drawSuperpositionedObject(Graphics2D g2d, WindowParameters w, int xGrid, int yGrid);
	
	//////////////////////////////////////////////////////////
	// Public methods
	//////////////////////////////////////////////////////////
	
	/**
	 * Increments the number of places where this superpositioned object exists on the game board.
	 */
	public void incrementCount() { 
		visibleCount++;
		
		if(visibleCount >= generatedCount) {
			generatedCount = visibleCount;
		}
	}
	
	/**
	 * Decrements the number of places where this superpositioned object exists on the game board.
	 */
	public void decrementCount() { visibleCount--; }
	
	/**
	 * Returns current number of places where this superpositioned object is visibly drawn on-screen.
	 * 
	 * @return
	 */
	public int getVisibleCount() { return visibleCount; }
	
	/**
	 * Returns the total number of on-screen objects that this is configured to draw.
	 * 
	 * @return
	 */
	public int getGeneratedCount() { return generatedCount; }
	
	/**
	 * Restores all consumed objects back to the game board, and resets the visible count.
	 */
	public void reset() {
		objectLocationTracker.restoreConsumedObjects(this);
	}
	
	//////////////////////////////////////////////////////////
	// Overridden methods
	//////////////////////////////////////////////////////////	
	
	/*
	 * (non-Javadoc)
	 * @see pacMan.ConsumableObject#consumeObject(pacMan.Actor)
	 */
	@Override
	public void consumeObject(Actor consumer) {
		// Check the consumer location and tell objLocationGrid to remove
		// pellet from that location.
		objectLocationTracker.removeStationaryObject(this, consumer.getGridLocationX(), consumer.getGridLocationY());
		consumer.addToScore(this.getPointValue(), false);
		if(wakaWakaToggle) {
			SoundEffect.EVENT_PACMAN_EATPELLET_EVEN.play();
		} else {
			SoundEffect.EVENT_PACMAN_EATPELLET_ODD.play();
			
		}
		wakaWakaToggle = !wakaWakaToggle;
	}
	
	/*
	 * (non-Javadoc)
	 * @see pacMan.BasicDrawableObject#getGridLocationX()
	 */
	@Override
	public int getGridLocationX() {
		System.err.println("Attempted to query location for an object that exists in multiple grid locations.");
		System.exit(1);
		return -1;
	}
	
	/*
	 * (non-Javadoc)
	 * @see pacMan.BasicDrawableObject#getGridLocationY()
	 */
	@Override
	public int getGridLocationY() {
		System.err.println("Attempted to query location for an object that exists in multiple grid locations.");
		System.exit(1);
		return -1;
	}
	
	/*
	 * (non-Javadoc)
	 * @see pacMan.BasicDrawableObject#drawObject(java.awt.Graphics2D)
	 */
	@Override
	public void drawObject(Graphics2D d) {
		objectLocationTracker.drawStationaryObject(d, this.getWindowParams(), this);
	}
	
	
	//////////////////////////////////////////////////////////
	// Private member variables
	//////////////////////////////////////////////////////////
	
	private boolean wakaWakaToggle = false;
	private ObjectLocationTracker objectLocationTracker;
	private int visibleCount;
	private int generatedCount;
}
